local function Fn()
    local Fx = Class()


    function Fx:Init(NPC_BASE_LIB)
        ---------------------------------------------------------------------------------------------------------------------------

        local function For_Sale_Task_Init(inst)
            ------------------------------------------------------------------------------
            inst.____For_Sale_Watch_World_State_fn = function()   ------------------- 每天执行概率判断和计时器重置
                if TUNING.NPC_MOD_TEST_DEBUG == true then   ---------------- 测试模式

                    inst.npc_everything_data:Set("time",0)
                    inst.npc_everything_data:Add("Buyer_Prop",1)    ------ 买家出现概率逐渐升高
                    local temp_prop = math.random(1000)/10
                    print("info : Buyer daily prop",temp_prop)
                    if temp_prop <= inst.npc_everything_data:Add("Buyer_Prop",100) and inst.npc_everything_data:Get("Buyer_Ready") ~= true then
                        inst.npc_everything_data:Set("Buyer_Ready",true)
                        -- local display_time = math.random(400)
                        local display_time = math.random(10)
                        print("info : Buyer Spawn time",display_time)
                        inst.npc_everything_data:Set("Buyer_Time",display_time)
                    end
                    
                else    ---------------------------------------------------------------------- 非测试模式
                    inst.npc_everything_data:Set("time",0)
                    inst.npc_everything_data:Add("Buyer_Prop",1)    ------ 买家出现概率逐渐升高
                    local temp_prop = math.random(1000)/10
                    if temp_prop <= inst.npc_everything_data:Add("Buyer_Prop",0) and inst.npc_everything_data:Get("Buyer_Ready") ~= true then
                        inst.npc_everything_data:Set("Buyer_Ready",true)
                        local display_time = math.random(400)
                        inst.npc_everything_data:Set("Buyer_Time",display_time)
                    end
                end


            end
            inst:WatchWorldState("cycles", inst.____For_Sale_Watch_World_State_fn)
            ------------------------------------------------------------------------------
            inst.____For_Scale_Task = inst:DoPeriodicTask(5,function()    ---------- 每5秒检查一次
                local time = inst.npc_everything_data:Add("time",5)
                if inst.npc_everything_data:Get("Buyer_Ready") == true and not inst:HasTag("Buyer_Show") and time >= inst.npc_everything_data:Add("Buyer_Time",0) then
                    if inst:IsNearPlayer(25) == false then
                        inst:PushEvent("Buyer_Show")
                    end
                end
                ----------------------------------------------------------------------
                -- -- 找最近的玩家，绑定给这个雕像，方便刷NPC
                local player = inst:GetNearestPlayer()
                if player ~= nil then
                    inst.__link_player = player
                end

            end)
            -- ------------------------------------------------------------------------------
            inst:ListenForEvent("Buyer_Show",function()
                inst:AddTag("Buyer_Show")                
                local pt = inst.npc_base_lib:GetSpawnPoint(Vector3(inst.Transform:GetWorldPosition()),1.5) or Vector3(inst.Transform:GetWorldPosition())
                local npc_name = "wilson"
                local player = inst.__link_player
                if player == nil then
                    -- player = TheSim:FindFirstEntityWithTag("player")
                    player = inst:GetNearestPlayer()
                    inst.__link_player = player
                end
                if player then
                    npc_name = player.npc_base_lib:Get_Random_Npc_Name()
                end
                local npc = SpawnPrefab(npc_name)
                npc.__link_player = player
                npc.buy_tar_item = inst
                npc.Ready = true
                inst.npc_base_lib:Add2Child(npc)
                npc.Transform:SetPosition(pt.x,0,pt.z)
                npc:AddDebuff("npc_event_item_buyer","npc_event_item_buyer")
                inst.____item_buyer = npc
            end)
            ------------------------------------------------------------------------------
        end


        function NPC_BASE_LIB:For_Sale_Sign_Show()
            if self.inst:HasTag("for_sale") then
                return
            end

            self.inst:DoTaskInTime(0.1,function()
                local sign = self.inst:SpawnChild("npc_fx_sign_for_sale")
                self.inst.____sale_sign = sign
                sign.Ready = true
            end)
            

            self.inst:AddTag("for_sale")
            For_Sale_Task_Init(self.inst)
            self.inst.npc_everything_data:Set("for_sale",true)
        end

        function NPC_BASE_LIB:For_Sale_Task_Cancel()
            if self.inst.____sale_sign then
                self.inst.____sale_sign:Remove()
            end
            self.inst:RemoveTag("for_sale")
            self.inst.npc_everything_data:Set("for_sale",false)
            if self.inst.____item_buyer then
                self.inst.____item_buyer:Remove()
            end
            if self.inst.____For_Scale_Task then
                self.inst.____For_Scale_Task:Cancel()
            end
            if self.inst.____For_Sale_Watch_World_State_fn then
                self.inst:StopWatchingWorldState("cycles",self.inst.____For_Sale_Watch_World_State_fn)
            end
            print("For_Sale_Task_Cancel",self.inst)            
        end


        function NPC_BASE_LIB:For_Sale_Immediately()
            self.inst:AddTag("for_sale")
            For_Sale_Task_Init(self.inst)
            self.inst:PushEvent("Buyer_Show")
        end

        ---------------------------------------------------------------------------------------------------------------------------
    end

    return Fx
end

return Fn